class_name EnemyPool
extends RefCounted

var active_enemies: Array = []
var inactive_enemies: Array = []

var enemy_scene: PackedScene
var enemy_count: int = 0
var max_enemies: int = 10

func init_pool(_enemy_scene: PackedScene, _max_enemies: int) -> void:
	self.enemy_scene = _enemy_scene
	self.max_enemies = _max_enemies

func get_enemy() -> Enemy_Impl:
	if enemy_scene == null:
		print("Enemy scene is not set.")
		return null

	if inactive_enemies.size() > 0:
		var enemy = inactive_enemies.pop_back()
		active_enemies.append(enemy)
		enemy.set_process(true)
		enemy.reset()
		return enemy
	else:
		if enemy_count < max_enemies:
			var new_enemy = enemy_scene.instantiate()
			active_enemies.append(new_enemy)
			enemy_count += 1
			return new_enemy
	return null

func release_enemy(enemy: Enemy_Impl) -> void:
	if enemy in active_enemies:
		enemy.get_parent().remove_child(enemy)
		enemy.set_process(false)
		active_enemies.erase(enemy)
		inactive_enemies.append(enemy)
	else:
		enemy.queue_free()
		print("Enemy not found in active enemies.")
